<?php
/**
 * Author: Hancock
 * Email: 84622365@qq.com
 * Date: 3/23/2016
 * Des: 控制器
 */
defined('INHkMvc') or exit('Access Invalid!');

class Controller
{
    public $view;
    public $output;
    public $tpl;

    public function __construct()
    {
        $query = empty($_SERVER['QUERY_STRING']) ? "con=" . DEFAULT_CON . "&act=" . DEFAULT_ACT : $_SERVER['QUERY_STRING'];
        Filter::url($query);
        $this->view = Factory::register('View', 'core');
    }

    public function display($tpl = '', $theme = '')
    {
        if ($theme == '' && $tpl == '') {
            $caller = debug_backtrace()[1];//获取调用函数
            $this->view->display(str_replace('Act', '', $caller['function']), $theme);
        } else {
            $this->view->display($tpl, $theme);
        }
    }

    public function layout($tpl = '', $layout = 'layout', $theme = '')
    {
        if ($tpl == '') {
            $caller = debug_backtrace()[1];
            $this->view->tpl = str_replace('Act', '', $caller['function']);
        } else {
            $this->view->tpl = $tpl;
        }
        $this->view->display($layout, $theme);
    }

    public function sign($key, $value)
    {
        $this->view->sign($key, $value);
    }

    protected function _getParam($key, $default=''){
        if(!array_key_exists($key, $_GET)) return $default;
        return $this->getRequestValue($_GET[$key]);
    }

    protected function _postParam($key, $default=''){
        if(!array_key_exists($key, $_POST)) return $default;
        return $this->getRequestValue($_POST[$key]);
    }

    protected function _getParams(){
        foreach ($_GET as $k => &$v){
            $v = $this->_getParam($k);
        }
        return $_GET;
    }

    protected function _postParams(){
        foreach ($_POST as $k => &$v){
            $v = $this->_postParam($k);
        }
        return $_POST;
    }

    private function getRequestValue($data){
        if(is_array($data)){
            foreach ($data as &$v){
                $v = $this->getRequestValue($v);
            }
        }else{
            $data = htmlspecialchars(addslashes(removeXss($data)));
        }
        return $data;
    }
    /**
    * 参数错误判断
    * @param $flag
    * @param $key
    */
    static protected function is_error_parameters($flag, $key="")
    {
        if (!$flag) {
            if($key=='token'){
                self::apiReturn('token','token is null',200);
            }else{
                self::apiReturn($_POST,'参数错误',200);
            }
        }
    }
    /**
     * json返回方法
     * @param  [type] $data 返回数据
     * @param  [type] $msg  返回信息
     * @param  [type] $code 返回代码 400：成功；200：参数错误；201：操作失败；
     * @return [type]       [description]
     */
    static protected function apiReturn($data, $msg, $code){
        $info = array();
        $info['data'] = $data;
        $info['msg'] = $msg ;
        $info['status'] = $code;
        if (isset($_GET['callback'])) {
            $msg=$_GET['callback'].'('.json_encode($info).')';
        } else {
            $msg=json_encode($info);
        }
        header('Content-Type:application/json; charset=utf-8');
        exit($msg);
    }
}
